#!/usr/bin/env python
# coding: utf-8

import os
import sys

"""the first parameter is the vcf file out from GATK
the second parameter is the path of IMPACT scripts, always '/mnt/ilustre/app/medical/.data/impact'
example: union-AF.py snp.vcf /mnt/ilustre/app/medical/.data/impact"""

assert "2.7" in sys.version

invcf = os.path.abspath(os.path.expanduser(sys.argv[1]))
data = os.path.abspath(os.path.expanduser(sys.argv[2]))

name = os.path.basename(invcf)[:-4]

# os.system("perl {}/change_gt_values.pl {} > {}_cleaned.vcf".format(data, invcf, name))
# os.system("format_af.sh -p{0} {0}_cleaned.vcf".format(name))
# os.system("anno_info_vcf.pl {0}_cleaned.vcf {0}.af.txt > {0}.af.vcf".format(name))

# for af and annovar, input vcf
os.system("perl /mnt/ilustre/app/medical/tools/annovar/bin/table_annovar.pl {0} /mnt/ilustre/app/medical/tools/annovar/hg19 -buildver hg19 -out {1}.af -remove -protocol knowngene,refGene,cosmic70,ljb26_all,esp6500si_all,snp138,1000g2014oct_all -operation g,g,f,f,f,f,f -vcfinput".format(invcf, name))


# for annovar anno
os.system("perl /mnt/ilustre/app/medical/tools/annovar/bin/convert2annovar.pl -format vcf4 --includeinfo {0} > {1}.avinput".format(invcf, name))

os.system("perl /mnt/ilustre/app/medical/tools/annovar/bin/table_annovar.pl {0}.avinput /mnt/ilustre/app/medical/tools/annovar/hg19 -buildver hg19 -out {0} -remove -protocol knowngene,refGene,cosmic70,ljb26_all,esp6500si_all,snp138,1000g2014oct_all -operation g,g,f,f,f,f,f ".format(name))


os.system("perl {0}/collect_dinuc.pl {1}.hg19_multianno.txt > {1}.hg19_multianno.ann.txt".format(data, name))
os.system("perl {0}/single_remove_synon.pl {1}.hg19_multianno.ann.txt > {1}.nonsyn.txt".format(data, name))
os.system("perl {0}/remove_over.01.one.pl {1}.nonsyn.txt > {1}.1000G.remove.txt".format(data, name))
os.system("perl {0}/remove_over.01.two.pl {1}.1000G.remove.txt > {1}.dbsnp.remove.txt".format(data, name))
os.system("perl {0}/get_deleterious.pl {1}.dbsnp.remove.txt > {1}.deleterious.txt".format(data, name))

os.system("loc-AF.py {0}.deleterious.txt {0}.af.hg19_multianno.txt {0}.af.deleterious.txt".format(name))
os.system("drugs.py {0}.af.deleterious.txt {1} {0}".format(name, data))
